﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using QunYing.BLL;

namespace Client
{
    public partial class FrmMsgManager : BaseChildForm
    {
        public static FrmMsgManager frm = null;
        public FrmMsgManager()
        {
            InitializeComponent();
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                this.sharpGrid1.AddPopupMenu(new ToolStripSeparator(), null);
                this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("删除留言"), RemoveMsg);
            }

            cbxState.SelectedIndex = 1;
            frm = this;
        }

        private void FrmMsgManager_Load(object sender, EventArgs e)
        {
            QueryData();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        public void QueryData()
        {
            QunYing.BLL.LeaveMsg msg = new QunYing.BLL.LeaveMsg();
            DateTime startDate = Convert.ToDateTime("1900-01-01");
            DateTime endDate = Convert.ToDateTime("2079-06-06");
            if (this.dateSpanOrder.StartDateEdit.EditValue != null)
            {
                startDate = Convert.ToDateTime(this.dateSpanOrder.StartDateEdit.EditValue);
            }
            if (this.dateSpanOrder.EndDateEdit.EditValue != null)
            {
                endDate = Convert.ToDateTime(this.dateSpanOrder.EndDateEdit.EditValue).AddHours(23).AddSeconds(59).AddMinutes(59);
            }
            string isDealer = GlobalInfo.UserLoginInfo.角色 == "经销商" ? "是" : "否";
            DataTable dt = msg.GetMsgByDealer(GlobalInfo.UserLoginInfo.用户ID, startDate, endDate,
                cbxState.EditValue == null ? "" : cbxState.EditValue.ToString(), cbxType.EditValue == null ? "" : cbxType.EditValue.ToString(),isDealer);

            sharpGrid1.DataSource = dt;
            GridView view = this.sharpGrid1.MainView as GridView;
            view.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(view_CustomColumnDisplayText);
            view.Columns["留言时间"].DisplayFormat.FormatType = FormatType.DateTime;
            view.Columns["留言时间"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            view.Columns["回复时间"].DisplayFormat.FormatType = FormatType.DateTime;
            view.Columns["回复时间"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
            view.Columns["ID"].Visible = false;
            view.Columns["回复ID"].Visible = false;
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                view.Columns["经销商编号"].Visible = false;
            }
        }

        void view_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "留言内容" || e.Column.FieldName == "回复内容")
            {
                e.DisplayText = e.DisplayText.Replace("<br/>", "  ");
            }
        }

        public override void BindData()
        {
            QueryData();
        }

        private void RemoveMsg(object sender, EventArgs e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            if (selectedRows.Length == 0)
            {
                GlobalInfo.MsgBoxShowError("请选择你要删除的留言");
                return;
            }
            else
            {
                if (XtraMessageBox.Show(GlobalInfo.UserLookAndFeel, "确认删除选择留言吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                    return;
                foreach (var v in selectedRows)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;

                    QunYing.BLL.LeaveMsg msg = new LeaveMsg();
                    bool b = msg.DelMsg(row.Row["ID"].ToString());
                    if (!b)
                    {
                        GlobalInfo.MsgBoxShowError("删除失败");
                    }
                }

                QueryData();
            }
        }

        private void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] selectedRows = view.GetSelectedRows();
            foreach (var v in selectedRows)
            {
                if (v >= 0)
                {
                    object obj = view.GetRow(v);
                    DataRowView row = obj as DataRowView;
                    if (row == null)
                        continue;

                    FrmRepay frm = SingleObjManager.GetSingleObj<FrmRepay>();
                    frm.row = row;
                    frm.ShowDialog();
                    break;
                }
            }
        }

    }
}
